#include "ProjectManager/project.h"

/******************************************
 *              Public
 ******************************************/

    /******************************************
     *              Constructors
     ******************************************/

        Project::Project(string filePath) : Component(){
            Load(filePath);
        }

        Project::Project(string path, string name)  :   Component(){
            m_Item              =   new ComponentItem(name, this);
            m_Path              =   path;
            m_Name              =   name;
            m_CurrentProject    =   this;
        }

    /******************************************
     *              Methods
     ******************************************/

        void Project::Save(){
            xml::Document document;
            Save(document);
            document.Save(FileLocation().c_str());
        }

        string Project::FileLocation(){
            string path = ConvertPath();
            path+= "/"+Name()+".xml";
            return path;
        }

        void Project::Load(string path){

            xml::Document   document     = xml::Document(path);

            m_Name  = document.m_Root->GetElementByTagName("Name")->Value();
            m_Path  = document.m_Root->GetElementByTagName("Path")->Value();

            // J'utilise une fonction magique pour retirer les \t et \n des châines récupérés

            char chars[]="\t\n";

            for (unsigned int i = 0; i < strlen(chars); ++i)
            {
               // you need include <algorithm> to use general algorithms like std::remove()
                m_Name.erase (std::remove(m_Name.begin(), m_Name.end(), chars[i]), m_Name.end());
                m_Path.erase (std::remove(m_Path.begin(), m_Path.end(), chars[i]), m_Path.end());
            }

            m_Item              =   new ComponentItem(m_Name, this);

            xml::Element * components = document.m_Root->GetElementByTagName("Components");

            if(components != NULL){
                for(int i=0; i<components->Childs.size(); i++){
                    xml::Node *  node =   components->Child(i);
                    if(node!=NULL){
                        if(node->Type() ==  xml::ELEMENT_NODE){
                            xml::Element * el = (xml::Element*)node;

                            if(el->tagName   =="PixImage"){
                                PixImage * pix = new PixImage(*el);
                                m_Components.push_back(pix);
                                m_Item->appendRow(pix->m_Item);
                            }
                        }
                    }
                }
            }


            /*for(int i=0; i< doc.m_Root->GetElementByTagName("Components").Childs.size(); i++){
                if()
            }*/


            m_CurrentProject    =   this;
        }

        /******************************************
         *              Get
         ******************************************/

            const string & Project::Path() const {
                return m_Path;
            }

            const string & Project::Name() const {
                return  m_Name;
            }

            /******************************************
             *              Static
             ******************************************/

                 Project & Project::CurrentProject(){
                    return *m_CurrentProject;
                }

        /******************************************
         *              Set
         ******************************************/

    /******************************************
     *              Virtual
     ******************************************/

        void Project::Save(xml::Document & document){

            xml::Element* element  =   new xml::Element("Project");

            xml::Element * name     =   new xml::Element("Name");
            xml::Element * path     =   new xml::Element("Path");

            xml::Element * components   =   new xml::Element("Components");

            element->AppendChild(*name);
            element->AppendChild(*path);
            element->AppendChild(*components);

            name->AppendText(Name());
            path->AppendText(Path());

            SaveChilds(*components);

            document.m_Root=(element);
        }

        void Project::Save(ofstream & file){

        }

        QMenu * Project::GetContextualMenu(){
            QMenu * contextualMenu  =   new QMenu();
            QMenu * addMenu         =   new QMenu("Add");
            addMenu->addAction("Atlas");
            addMenu->addAction("Image");
            addMenu->addAction("TileMap");
            addMenu->addAction("Tile");

            contextualMenu->addMenu(addMenu);

            return contextualMenu;
        }

        void Project::ExeContextualMenu(QAction * selectedAction){
            if(selectedAction->text()=="Atlas"){
                NewAtlasComponent();
            }else if( selectedAction->text()=="Image"){
                NewImage();
            }
        }


/******************************************
 *              Private
 ******************************************/

    /******************************************
     *              Methods
     ******************************************/

        void Project::SaveChilds(xml::Element & element){
            for(int i=0; i< m_Components.size();i++){
                m_Components.at(i)->Save(element);
            }
        }

        string Project::ConvertPath(){
            string convertPath  =   m_Path;
            for(int i=0; i< convertPath.size();i++){
                if(convertPath[i]=='\\'){
                    convertPath[i]='/';
                }
            }
            return convertPath;
        }

    /******************************************
     *              Properties
     ******************************************/

        /******************************************
         *              Static
         ******************************************/

            Project* Project::m_CurrentProject   =   NULL;

/******************************************
 *              Slots
 ******************************************/

   void Project::CreateNewAtlas(int width, int height){
       Atlas * atlas = new Atlas(width, height);
       m_Item->appendRow(atlas->m_Item);

       connect(atlas,SIGNAL(SendCanvas(Atlas*)), this, SLOT(ShowAtlas(Atlas*)));
       connect(atlas, SIGNAL(SendInspector(QWidget*)), this, SLOT(SetInspectorWidget(QWidget*)));
   }

   void Project::NewAtlasComponent(){
       NewAtlas * atlas =   new NewAtlas();
       connect(atlas, SIGNAL(Validation(int,int)), this, SLOT(CreateNewAtlas(int,int)));
   }

   void Project::NewImage(){
        QString path    =   QFileDialog::getOpenFileName();

        int index   =   0;
        int i       =   path.size();

        while ( i> 0 && path[i]!='/'){
            i--;
        }
        index=i;

        PixImage    *   image   =   new PixImage(path, path.right(path.size()-index-1));
        m_Components.push_back(image);
        m_Item->appendRow(image->m_Item);
   }
